/************************* 版权声明 *********************************
 * 
 * 版权所有：Apache License 2.0
 * 
 ************************* 版本声明 *********************************
 *@since jdk1.5
 *@version 0.1
 ************************* 变更记录 *********************************
 *
 * 创建者：bijiqiang Email:bijiqiang@gmail.com   创建日期： 2010-1-7
 * 创建记录：创建类结构。
 * 
 * 修改者：       修改日期：
 * 修改记录：
 *
 * 修改者：       修改日期：
 * 修改记录：
 * ……
 ************************* To  Do *********************************
 *
 * 1、实现所有未实现的方法（抛出UnsupportedOperationException的方法）。
 * 
 ************************* 随   笔 *********************************
 *
 * 
 * 
 ******************************************************************
 */

package com.realgodo.site.web.admin.user;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springside.modules.orm.Page;

import com.realgodo.site.common.MailService;
import com.realgodo.site.entity.security.User;
import com.realgodo.site.service.UserManager;
import com.realgodo.site.web.admin.AdminDefaultController;

@Scope("prototype")
@Results( { @Result(name = "toindex", type = "redirect", location = "${base}/admin/user/") })
public class IndexController extends AdminDefaultController {

	private static final long serialVersionUID = -8573249598653298410L;

	@Autowired
	private UserManager userManager;
	@Autowired
	private MailService mailService;
	private Page<User> userPage = new Page<User>(PAGE_SIZE);
	private User user;
	private User userForm = new User();

	public String execute() throws Exception {
		userForm.setEnable(User.ENABLED_DISENABLED_ALL);
		userForm.setMeiMeiMan(User.MEIMEI_MAN_ALL);
		userForm.setMeiMeiMember(User.MEIMEI_MEMBER_ALL);
		userForm.setRecommend(User.RECOMMEND_ALL);
		userPage
				.setPageNo(userManager.isParamNotNull(getParamMap(), Page.PAGE) ? Integer
						.valueOf((String) userManager.getParamValue(
								getParamMap(), Page.PAGE))
						: 1);// 查询页数
		userPage = this.userManager.findUsers(userPage, userForm);
		super.setPageInfo(userPage);
		return SUCCESS;
	}

	public String detail() {
		if (null != getId()) {
			user = this.userManager.get(getId());
		}
		return "detail";
	}

	public String lock() throws Exception {
		if (null != getId()) {
			this.userManager.lockUser(getId());
		}
		return "toindex";
	}

	public String enable() throws Exception {
		if (null != getId()) {
			this.userManager.enableUser(getId());
		}
		return "toindex";
	}

	public String man() throws Exception {
		if (null != getId()) {
			this.userManager.manUser(getId());
		}
		return "toindex";
	}

	public String unMan() throws Exception {
		if (null != getId()) {
			this.userManager.unManUser(getId());
		}
		return "toindex";
	}

	public String member() throws Exception {
		if (null != getId()) {
			this.userManager.memberUser(getId());
		}
		return "toindex";
	}

	public String unMember() throws Exception {
		if (null != getId()) {
			this.userManager.unMemberUser(getId());
		}
		return "toindex";
	}

	public String recommend() throws Exception {
		if (null != getId()) {
			this.userManager.recommendUser(getId());
		}
		return "toindex";
	}

	public String unRecommend() throws Exception {
		if (null != getId()) {
			this.userManager.unRecommendUser(getId());
		}
		return "toindex";
	}

	/** 审核用户通过 */
	public String auth() {
		User user = userManager.get(id);
		user.setStauts(User.STAUTS_AUTH);
		userManager.save(user);
		this.mailService.sendAuthMail(user);
		return "toindex";
	}

	/** 审核用户拒绝 */
	public String reject() {
		User user = userManager.get(id);
		user.setStauts(User.STAUTS_REJECT);
		userManager.save(user);
		this.mailService.sendRejectMail(user);
		return "toindex";
	}

	// -------------get/set-------------------//

	public Page<User> getUserPage() {
		return userPage;
	}

	public void setUserPage(Page<User> userPage) {
		this.userPage = userPage;
	}

	public User getUserForm() {
		return userForm;
	}

	public void setUserForm(User userForm) {
		this.userForm = userForm;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
